<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>suite</title>
		<link rel="stylesheet" type="text/css" href="/scripts/jquery-easyui-1.6.6/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="/scripts/jquery-easyui-1.6.6/themes/icon.css">
		<script type="text/javascript" src="/scripts/jquery-easyui-1.6.6/jquery.min.js"></script>
		<script type="text/javascript" src="/scripts/jquery-easyui-1.6.6/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="/scripts/jquery-easyui-1.6.6/locale/easyui-lang-zh_CN.js"></script>
		<script type="text/javascript" src="/scripts/project.js"></script>

	</head>
	<script type="text/javascript">
		var pid = localStorage.getItem("pid");
		$(function() {
			$.ajax({
				type: "get",
				url: pro_url + "/suites?pid=" + pid,
				success: function(data) {
					if(data.status==2000) {
						$('#suite_name').combobox({
							data: data.rows,
							valueField: 'id',
							textField: 'name',
							panelHeight: 'auto',
//							value: '1'
						})
					}
				}
			})

			$.ajax({
				type: "get",
				url: pro_url + "/modules?pid=" + pid,
				success: function(data) {
					console.log(data)
					if(data.status==2000) {
						$('#mod').combobox({
							data: data.rows,
							valueField: 'id',
							textField: 'mName',
							panelHeight: 'auto',
//							value: '1'
						})
					}
				}
			})

		})
	</script>

	<body>
		<div id="tb" style="padding: 3px;width: 1000px">
			<div style="width:500px;float:left">
				<span>模块:</span>
				<input class="easyui-combobox" id="mod" name="mod" panelHeight="auto" style="width: 120px">
				<input id="caseName" style="line-height: 26px; border: 1px solid #ccc" onkeydown="enterPress()">
				<a id="search" href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="doSearch()">查询</a>
			</div>
			<div style="width:400px; float:right">
				<span>测试集:</span>
				<input class="easyui-combobox" id="suite_name" name="suite_name" style="width:50%;">
				<a id="submit" href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="submit()" style="float:right">提交</a>
			</div>
		</div>
		<div style="text-align: left">
		<table style=" float:left;display:block;clear:both;">
			<tr>
				<td>
					<table id="case_scripts" class="easyui-datagrid" title="" style="width: 500px; height: 460px" data-options="rownumbers:true,singleSelect:false,method:'get',pagination:true">
						<thead>
							<tr>
								<th data-options="field:'ck',checkbox:true"></th>
								<th data-options="field:'module',width:80">模块名</th>
								<th data-options="field:'caseName',width:400">脚本名</th>
							</tr>
						</thead>
					</table>
				</td>
				<td>
					<a id="add" class="easyui-linkbutton" onclick="addToRight()" iconCls="icon-add">>></a> <br /> <br /> <br />
					<a id="remove" class="easyui-linkbutton" onclick="remove()" iconCls="icon-remove">
						<< </a>
				</td>
				<td>
					<table id="suitelist" class="easyui-datagrid" title="" style="width: 500px; height: 460px" data-options="rownumbers:true,singleSelect:false,pagination:true">
						<thead>
							<tr>
								<th data-options="field:'ck',checkbox:true"></th>
								<th data-options="field:'caseName',width:400">脚本名</th>
							</tr>
					</table>
				</td>
			</tr>
		</table>
		</div>
	</body>

	<script type="text/javascript">
		var sid; //当前编辑的测试集id
		$('#suite_name').combobox({
			onSelect: function(s_data) {
				console.log(s_data)
				sid = s_data.id
				$.ajax({
					type: "get",
					url: pro_url + "/case/suite?sid=" + sid,
					async: true,
					success: function(c_data) {
						if(c_data) {
							$('#suitelist').datagrid('loadData', c_data);
						}
						console.log(c_data);
					}
				});
			}
		})

		$('#mod').combobox({
			onSelect: function(m_data) {
				console.log(m_data)
				mid = m_data.id
				$.ajax({
					type: "get",
					url: pro_url + "/case/module?pid=" + pid + "&module=" + mid,
					async: true,
					success: function(c_data) {
						if(c_data) {
							$('#case_scripts').datagrid('loadData', c_data);
						}
						console.log(c_data);
					}
				});
			}
		})

		//		$('#case_scripts').datagrid({
		//			url: pro_url + '/cases?pid=' + pid
		//		})

		function doSearch() {
			var mod = $('#mod').combobox('getText');
			var s_name = $('#caseName').val();
			$.ajax({
				url: pro_url + "/case/search?pid=" + pid + "&module=" + mod + "&cases=" +
					s_name,
				type: "GET",
				success: function(data) {
					if(data) {
						$('#case_scripts').datagrid('loadData', data);
					};
					console.log(data);
				}
			});

		}

		var add_list = new Array();
		var del_list = new Array();

		function addToRight() {
			var sel = $('#case_scripts').datagrid("getSelections");
			for(var i = 0; i < sel.length; i++) {
				$('#suitelist').datagrid('appendRow', sel[i]);
				var rowIndex = $('#case_scripts').datagrid("getRowIndex", sel[i]);
				$('#case_scripts').datagrid("deleteRow", rowIndex);
				add_list.push(sel[i]);
			}
		}

		function remove() {
			var sel = $('#suitelist').datagrid("getSelections");
			for(var i = 0; i < sel.length; i++) {
				var rIndex = $('#suitelist').datagrid("getRowIndex", sel[i]);
				$('#suitelist').datagrid("deleteRow", rIndex);

				del_list.push(sel[i]);
				var index = add_list.indexOf(sel[i]);
				if(index > -1) {
					add_list.splice(index, 1);
					$('#case_scripts').datagrid('appendRow', sel[i]); //如果移除刚才新加的才往左边加一行，之前该测试集就有的脚本去除，不往左边添加
				}
			}
		}

		function submit() {
			var param = {
				suite: sid,
				add: add_list,
				del: del_list
			};
			$.ajax({
				url: pro_url + "/sc/edit",
				type: 'POST',
				data: JSON.stringify(param),
				dataType: 'JSON',
				contentType: "application/json;charset=UTF-8",
				success: function(data) {
					console.log(data)
					if(data.headers.status == 2000) {
						alert('该测试集编辑成功！')
					} else {
						alert('该测试集编辑失败！')
					}
				}
			})
		}
	</script>

</html>